package com.qidian.mes.qc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.pojo.Defect;
import com.qidian.mes.qc.mapper.DefectMapper;
import com.qidian.mes.qc.service.DefectService;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class DefectServiceImpl implements DefectService {
  @Autowired
  private DefectMapper defectMapper;

    @Override
    public MESResult queryById(Long defectId) {
      Defect defect=null;
      if ((defect=defectMapper.selectById(defectId))!=null){
        return MESResult.ok(defect);
      }
        return MESResult.empty();
    }

  @Override
  public MESResult list(int page, int limit, Defect defect) {
    // 开启分页 在当前线程中执行下一条SQL的时候就会进行分页。
    PageHelper.startPage(page,limit);
    List<Defect>list =defectMapper.list(defect);
    // 组织一个分页对象。（页码，每页大小，最大页码，数据，总条数）
    PageInfo<Defect> pageInfo=new PageInfo<>(list);
    return MESResult.ok(pageInfo);
  }

  @Override
  public MESResult save(Defect defect) {
      //查重
    if (defectMapper.selectByDefectName(defect.getDefectName())!=null ){
      return MESResult.duplicate("缺陷描述重复");
    }
    defectMapper.save(defect);
    return MESResult.ok();
  }

  @Override
  public MESResult update(Defect defect) {
    // 校验是否重复
    Defect  Temp = defectMapper.selectByDefectName(defect.getDefectName());
    if( Temp != null && ! Temp.getDefectId().equals(defect.getDefectId())){
      return MESResult.duplicate("缺陷名重复");
    }

    defectMapper.update(defect);
    return MESResult.ok();
  }

  @Override
  public MESResult deleteByDefectId(Long defectId) {

    defectMapper.deleteByDefectId(defectId);
    return MESResult.ok();
  }

  @Override
  public MESResult deletesByDefectIds(Long[] defectId) {
    int count = defectMapper.deletesByDefectId(defectId);

    return MESResult.ok(count+"");
  }


}


